#!/bin/bash

echo "本地安装 Kafka (无需 Docker)"
echo "====================================="

# 检查 Java
if ! command -v java &> /dev/null; then
    echo "错误: 需要安装 Java"
    echo "请先安装 Java: brew install openjdk"
    exit 1
fi

# 创建目录
mkdir -p kafka-local
cd kafka-local

# 下载 Kafka (如果不存在)
if [ ! -f "kafka_2.13-2.8.1.tgz" ]; then
    echo "下载 Kafka..."
    curl -O https://archive.apache.org/dist/kafka/2.8.1/kafka_2.13-2.8.1.tgz
fi

# 解压 (如果不存在)
if [ ! -d "kafka_2.13-2.8.1" ]; then
    echo "解压 Kafka..."
    tar -xzf kafka_2.13-2.8.1.tgz
fi

cd kafka_2.13-2.8.1

echo ""
echo "Kafka 已准备就绪!"
echo ""
echo "启动步骤:"
echo "1. 启动 Zookeeper:"
echo "   bin/zookeeper-server-start.sh config/zookeeper.properties"
echo ""
echo "2. 启动 Kafka (新终端):"
echo "   bin/kafka-server-start.sh config/server.properties"
echo ""
echo "3. 创建 topic:"
echo "   bin/kafka-topics.sh --create --topic test --bootstrap-server localhost:9092"
echo ""
echo "4. 生产消息:"
echo "   bin/kafka-console-producer.sh --topic test --bootstrap-server localhost:9092"
echo ""
echo "5. 消费消息:"
echo "   bin/kafka-console-consumer.sh --topic test --from-beginning --bootstrap-server localhost:9092"